package com.situ.hotel.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.situ.hotel.entity.Order;
import com.situ.hotel.entity.Room;

@Mapper
//  mybatis 的注解    启动时扫描 放入spring容器
//  其他地方直接使用
public interface RoomMapper {
	/*
	 * 添加房间 value 速度略快于values insert 下可以使用@options 获取数据库的生成主键
	 */
	@Insert({ "insert into room (number,type,dscp,price,pic)", "value(#{number},#{type},#{dscp},#{price},#{pic})" })
	@Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
	// 向数据表中插入数据时，获取自动生成的主键，再将之设置到参数room的id属性中
	int insert(Room room);

	@Select("select * from room where number=#{number}")
	Room selectByNumber(String number);

	@Select({ "<script>", "select * from room", "<where>",
			"<if test='number!=null and number.length>0'>number=#{number} </if>",
			"<if test='type!=null'> and type=#{type}  </if>",
			"<if test='dscp!=null and dscp.length>0'> and dscp like '%${dscp}%' </if>",
			"<if test='status!=null'>and status=#{status}</if>", "</where>", "</script>" })
	List<Room> select(Room room);

	@Update({ "update room set number=#{number},type=#{type},dscp=#{dscp},price=#{price},pic=#{pic},",
			"status=#{status}", "where id = #{id}" })
	int update(Room room);

	@Select("select * from room where id=#{id}")
	Room selectById(int id);

	@Select({ "<script>",
			"select * from room", 
			"<where>",
			"<if test='room.number!=null and room.number.length>0'> " 
			+ " and room.number=#{room.number}</if>",
			"<if test='room.type!=null'> and room.type=#{room.type}</if>",
			"<if test='room.dscp!=null and room.dscp.length>0'> and room.dscp like '%${room.dscp}%'</if>",
			"and room.status=0",
			"<if test='order.start!=null and order.end!=null'>", 
			"and room.id not in( ",
			"  select room_id from `order` ", 
			" where `start` &lt;= #{order.start}", 
			" and  `end` &gt;= #{order.start}",
			"or `start` &lt;= #{order.end}", 
			"and `end` &gt;= #{order.end}", 
			"or `start`  &gt;= #{order.start}",
			" and `end` &lt;= #{order.end}",
			")", 
			"</if>", 
			"</where>", 
			"</script>"
	})
	List<Room> selectByOrder(@Param("room") Room room, @Param("order") Order order);
}
